/*
* ExaminationDaoImpl.java
* Created on  2014-8-4 上午9:41
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-8-4       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.task.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.task.dao.ExaminationDao;
import com.genghis.ptas.task.entity.ExaminationInfo;
import com.genghis.ptas.task.entity.ExaminationTask;
import com.genghis.ptas.task.entity.LibraryOption;
import com.genghis.ptas.task.entity.LibraryTask;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Repository("examinationDao")
public class ExaminationDaoImpl extends PageDao implements ExaminationDao {
    @Override
    public Page<ExaminationInfo> findExaminationPageList(PageParameter pageParameter, ExaminationInfo examinationInfo) {
        StringBuilder sql = new StringBuilder("SELECT * FROM examination_info where 1=1 ");
        if (examinationInfo.getExaminationName() != null && !examinationInfo.getExaminationName().equals("")) {
            sql.append(" AND examination_name like '%").append(examinationInfo.getExaminationName().trim()).append("%'");
        }
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(examinationInfo), new BeanPropertyRowMapper<ExaminationInfo>(ExaminationInfo.class));
    }

    @Override
    public void deleteExaminationInfoById(Integer id) {
        String sql = "DELETE FROM examination_info WHERE id = ?";
        getJdbcTemplate().update(sql, id);
    }

    @Override
    public void deleteExaminationTaskByExaminationInfoId(Integer examinationInfoId) {
        String sql = "DELETE FROM examination_task WHERE examination_info_id = ?";
        getJdbcTemplate().update(sql, examinationInfoId);
    }

    @Override
    public int addExamination(ExaminationInfo examinationInfo) {
        String sql = "INSERT INTO examination_info(examination_name,establish_data,examination_type) " +
                "VALUES(:examinationName, :establishData,:examinationType)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(examinationInfo), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public ExaminationInfo findExaminationById(int taskId) {
        String sql = "SELECT * FROM examination_info WHERE id = ?";
        return getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<ExaminationInfo>(ExaminationInfo.class), taskId);
    }

    @Override
    public List<LibraryOption> findLibraryOptionByTaskId(int taskId) {
        String sql = "SELECT * FROM library_option WHERE task_id = ?";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<LibraryOption>(LibraryOption.class), taskId);
    }

    @Override
    public void editExamination(ExaminationInfo libraryTask) {
        String sql = "UPDATE library_task SET title = :title, task_type = :taskType , answer = :answer, knowledge_point = :knowledgePoint WHERE id = :id";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(libraryTask));
    }

    @Override
    public int addExaminationTask(ExaminationTask examinationTask) {
        String sql = "INSERT INTO examination_task(examination_info_id,library_task_id,task_score)VALUES(:examinationInfoId, :libraryTaskId, :taskScore)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(examinationTask), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public List<ExaminationTask> findExaminationTaskByExaminationInfoId(Integer id) {
        String sql = "SELECT * FROM examination_task WHERE examination_info_id = ?";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<ExaminationTask>(ExaminationTask.class), id);
    }

    @Override
    public List<ExaminationInfo> findAllExaminationList() {
        String sql = "SELECT * FROM examination_info WHERE 1=1";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<ExaminationInfo>(ExaminationInfo.class));
    }
}
